@model Admin.NET.Application.Service.System.LowCode.Dto.Front_CodeGenerate
using @(Model.NameSpace).Entity;
using Admin.NET.Core;
using Admin.NET.Core.Util.LowCode;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace @Model.NameSpace
{
    /// <summary>
    /// @(@Model.BusName)服务
    /// </summary>
    [Route("api")]
    [ApiDescriptionSettings("自己的业务", Name = "@Model.ClassName", Order = 100)]
    [Route("api/[Controller]")]
    public class @(@Model.ClassName)Service : I@(@Model.ClassName)Service, IDynamicApiController, ITransient
    {
        private readonly IRepository<@(@Model.ClassName),@(@Model.DatabaseName)> _@(@Model.CamelizeClassName)Rep;
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk" && @column.FkEntityName != "" && @column.FkColumnName != ""){
        @:private readonly IRepository<@(@column.FkEntityName)> _@(@column.LowerFkEntityName)Rep;
}
}

@if(@Model.IsFile)
{
        @:private readonly IRepository<SysFile> _sysFileInfoRep;
}

        public @(@Model.ClassName)Service(
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk" && @column.FkEntityName != "" && @column.FkColumnName != ""){
            @:IRepository<@(@column.FkEntityName)> @(@column.LowerFkEntityName)Rep,
}
}
            IRepository<@(@Model.ClassName),@(@Model.DatabaseName)> @(@Model.CamelizeClassName)Rep
@if(@Model.IsFile)
{
            @:,IRepository<SysFile> sysFileInfoRep
}
        )
        {
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk" && @column.FkEntityName != "" && @column.FkColumnName != ""){
            @:_@(@column.LowerFkEntityName)Rep = @(@column.LowerFkEntityName)Rep;
}
}
            _@(@Model.CamelizeClassName)Rep = @(@Model.CamelizeClassName)Rep;
@if(@Model.IsFile)
{
        @:_sysFileInfoRep = sysFileInfoRep;
}
        }

        /// <summary>
        /// 分页查询@(@Model.BusName)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<@(@Model.ClassName)Output>> Page([FromQuery] @(@Model.ClassName)Search input)
        {
            var @(@Model.CamelizeClassName)s = await _@(@Model.CamelizeClassName)Rep.DetachedEntities
@foreach (var column in Model.TableField){    
if (@column.QueryWhether == "Y"){
if (@column.NetType.ToLower() == "string"){
if (@column.QueryType == "like"){
                                     @:.Where(!string.IsNullOrEmpty(input.@column.ColumnName), u => EF.Functions.Like(u.@(@column.ColumnName), $"%{input.@(@column.ColumnName).Trim()}%"))
} else {
                                     @:.Where(!string.IsNullOrEmpty(input.@column.ColumnName), u => u.@(@column.ColumnName) @column.QueryType input.@(@column.ColumnName))
}
} else {
if (@column.EffectType == "fk"){                
                                     @:.Where(input.@column.ColumnName > 0, u => u.@(@column.ColumnName) @column.QueryType input.@(@column.ColumnName))
} else {
                                     @:.Where(input.@column.ColumnName != null, u => u.@(@column.ColumnName) @column.QueryType input.@(@column.ColumnName))
}
}
}
}
                                     .OrderBy(PageInputOrder.OrderBuilder<@(@Model.ClassName)Search>(input))
                                     .ProjectToType<@(@Model.ClassName)Output>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
@if(@Model.IsFile)
{
            @:@(@Model.CamelizeClassName)s.Rows.ToList().ForEach(item =>
            @:{
foreach (var column in Model.FileTableField){  
                @:item.@(@column.ColumnName) = item.@(@column.ColumnName)_Str.GetFiles(_sysFileInfoRep);
}
            @:});
}
            return @(@Model.CamelizeClassName)s;
        }

        /// <summary>
        /// 增加@(@Model.BusName)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task Add(Add@(@Model.ClassName)Input input)
        {
            var @(@Model.CamelizeClassName) = input.Adapt<@(@Model.ClassName)>();
            await _@(@Model.CamelizeClassName)Rep.InsertAsync(@(@Model.CamelizeClassName));
        }

        /// <summary>
        /// 删除@(@Model.BusName)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task Delete(Delete@(@Model.ClassName)Input input)
        {
@foreach (var column in Model.TableField){
if (@column.ColumnKey == "True"){
            @:var @(@Model.CamelizeClassName) = await _@(@Model.CamelizeClassName)Rep.FirstOrDefaultAsync(u => u.@(@column.ColumnName) == input.@(@column.ColumnName));
}
}
            await _@(@Model.CamelizeClassName)Rep.DeleteAsync(@(@Model.CamelizeClassName));
        }

        /// <summary>
        /// 更新@(@Model.BusName)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task Update(Update@(@Model.ClassName)Input input)
        {
            var isExist = await _@(@Model.CamelizeClassName)Rep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var @(@Model.CamelizeClassName) = input.Adapt<@(@Model.ClassName)>();
            await _@(@Model.CamelizeClassName)Rep.UpdateAsync(@(@Model.CamelizeClassName),ignoreNullValues:true);
        }

        /// <summary>
        /// 获取@(@Model.BusName)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<@(@Model.ClassName)Output> Get([FromQuery] Querye@(@Model.ClassName)Input input)
        {
@foreach (var column in Model.TableField){
if (@column.ColumnKey == "True"){
            @:return (await _@(@Model.CamelizeClassName)Rep.DetachedEntities.FirstOrDefaultAsync(u => u.@(@column.ColumnName) == input.@(@column.ColumnName))).Adapt<@(@Model.ClassName)Output>();
}
}            
        }

        /// <summary>
        /// 获取@(@Model.BusName)列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<List<@(@Model.ClassName)Output>> List([FromQuery] @(@Model.ClassName)Input input)
        {
            return await _@(@Model.CamelizeClassName)Rep.DetachedEntities.ProjectToType<@(@Model.ClassName)Output>().ToListAsync();
        }    
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk" && @column.FkEntityName != "" && @column.FkColumnName != ""){
        @:
        @:/// <summary>
        @:/// 获取@(@column.FkEntityName)列表
        @:/// </summary>
        @:/// <returns></returns>
        @:[HttpGet("fk@(@column.FkEntityName)")]
        @:public async Task<dynamic> Fk@(@column.FkEntityName)List()
        @:{
            @:var list = await _@(@column.LowerFkEntityName)Rep.DetachedEntities.ToListAsync();
            @:return list.Select(e => new {Code = e.Id, Name = e.@(@column.FkColumnName)});
        @:}
}
}

    }
}
